﻿using BPMN.Application.IService;
using BPMN.Domain;
using BPMN.Domain.Entity.User;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using BPMN.DomianShare;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using BPMN.Application.Contracts.DTO.Request.UserInfo;
using BPMN.Application.Contracts.DTO.Request.Role;
using BPMN.Application.Contracts.DTO.Response.Role;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.Contracts.DTO.Request.PerRole;
using BPMN.Application.Contracts.DTO.Response.RolePermission;
using BPMN.Domain.Entity.Custom;
using BPMN.Application.Contracts.DTO.Request.Custom;
using BPMN.Application.Contracts.DTO.Response.Custom;
using BPMN.Application.Contracts.BaseWay;

namespace BPMN.Application.Service
{
    public class CustomAppService : AppServiceBase<Custom>, ICustomAppService
    {
        private readonly IRepositoryBase<Custom> RepCustom;
        private readonly IMapper mapper;

        public  CustomAppService(IRepositoryBase<Custom> RepCustom, IMapper mapper):base(RepCustom)
        {
            this.RepCustom = RepCustom;
            this.mapper = mapper;
        }

        public async Task<ResultResponseDto<int>> AddCustomInfo(CustomRequstDto entity)
        {
            var count=RepCustom.GetAll().Result.Where(x=>x.FormCode== entity.FormCode).Count();
            var n = 0;
            if (count>0)
            {
                return new ResultResponseDto<int>()
                {
                    Code = ResultCode.Fail,
                    ErrorInfo = "已经存在",
                };
            }
            else
            {
                var obj=mapper.Map<Custom>(entity);
                n=await RepCustom.Add(obj);
            }
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }

        public async Task<ResultResponseDto<int>> DeleleCustomInfo(int Customid)
        {
            var entity=RepCustom.GetAll().Result.Where(x => x.NumberRule_Id == Customid);
            await RepCustom.RealDel(entity.AsEnumerable());
            return new ResultResponseDto<int>();
        }

        public async Task<ResultResponseDto<List<CustomShowResponseDto>>> GetCustomInfo()
        {
            var list=await RepCustom.GetAll();
            return new ResultResponseDto<List<CustomShowResponseDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功！",
                Data = mapper.Map<List<CustomShowResponseDto>>(list)
            };
        }
        public async Task<string> GetProcessLineCode()
        {
            DateTime dateNow = DateTime.ParseExact
                (DateTime.Now.ToString("yyyy-MM-dd"), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
            //只返回一条数据   FormCode
            string defectItemCode = (await RepCustom.GetAll()).Where(x => x.FormCode.Length > 8 && x.CreateDate > dateNow)
                .OrderByDescending(x => x.CreateDate)
                .Select(x => x.FormCode).FirstOrDefault();
            var numberRule = (await RepCustom.GetAll()).Where(x => x.FormCode == "YH2023001").OrderByDescending(x => x.CreateDate).FirstOrDefault();
            if(numberRule != null)
            {
                string rule = numberRule.Prefix + DateTime.Now.ToString(numberRule.SubmitTime.Replace("hh","HH"));
                if(string.IsNullOrEmpty(defectItemCode) )
                {
                    rule += "1".PadLeft(numberRule.SerialNumber,'0');
                }
                else
                {
                    rule += (int.Parse(defectItemCode.Substring(defectItemCode.Length - numberRule.SerialNumber)) + 1).ToString("0".PadLeft(numberRule.SerialNumber,'0'));
                }
                return rule;
            }
            else
            {
                return DateTime.Now.ToString("yyyyMMddHHmmssff");
            }
        }

        public Task<ResultResponseDto<CustomShowResponseDto>> GetCustomInfoById(int Id)
        {
            throw new NotImplementedException();
        }

        public Task<ResultResponseDto<int>> UpdateCustomInfo(CustomRequstDto entity)
        {
            throw new NotImplementedException();
        }
        //

    }
}

